package ofpof.domain.dao.mapper;

import java.util.Date;

import ofpof.domain.Session;
import ofpof.domain.User;

import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Component
public class SessionMapper implements MongoMapper<Session> {

	@Override
	public Session fromDbObject(DBObject dbObject) {
		Session session = new Session();
		session.setCreatedOn((Date) dbObject.get("createdon"));
		session.setId((ObjectId) dbObject.get("_id"));
		User user = new User();
		session.setUser(user);
		user.setId(new ObjectId(dbObject.get("userid").toString()));
		
		return session;
	}

	@Override
	public DBObject toDbObject(Session entity) {
		DBObject dbObject  = new BasicDBObject();
		dbObject.put("_id",  entity.getId());
		dbObject.put("createdon",entity.getCreatedOn());
		dbObject.put("userid", entity.getUser().getId().toString());
		return dbObject;
	}

}
